package org.hepeng.fabric.gateway.event.spring;

import org.hepeng.fabric.gateway.event.ChainCodeEventListenerContainer;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @author he peng
 * @date 2022/3/30
 */

@Configuration
@Import(ChainCodeEventAutoConfiguration.CCEventListenerMethodProcessorRegistrar.class)
public class ChainCodeEventAutoConfiguration {

    @ConditionalOnMissingBean(ChainCodeEventListenerContainer.class)
    @Bean
    public ChainCodeEventListenerContainer chainCodeEventListenerContainer() {

        return new ChainCodeEventListenerContainer();
    }



    public static class CCEventListenerMethodProcessorRegistrar implements ImportBeanDefinitionRegistrar {

        @Override
        public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

            GenericBeanDefinition definition = new GenericBeanDefinition();
            definition.setBeanClass(CCEventListenerMethodProcessor.class);
            registry.registerBeanDefinition(CCEventListenerMethodProcessor.class.getSimpleName() , definition);
        }
    }
}
